//
//  ViewController.m
//  Test_03
//
//  Created by User-01 on 3/19/14.
//  Copyright (c) 2014 User-01. All rights reserved.
//

#import "ViewController.h"
#import "TaskData.h"
#import "Tasks.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property TaskData *tasks;
@property Tasks *task;
@property (weak, nonatomic) IBOutlet UILabel *dueDateLabel;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tasks = [[TaskData alloc] init];
    self.task = [[Tasks alloc] init];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    self.navigationItem.leftBarButtonItem = self.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}


- (IBAction)addTask:(id)sender {
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tasks.tasksArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"customCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    Tasks *tmpTask = [self.tasks.tasksArr objectAtIndex: indexPath.row];
    cell.textLabel.text = tmpTask.taskText;
    
    
    // DueDate text
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"mm/dd/yyyy hh:mm"];
    
    NSString *stringFromDate = [dateFormat stringFromDate:tmpTask.dueDate];
    NSString *format = [[NSString alloc] initWithFormat:@"Due date: "];
    if (stringFromDate == nil) {
        stringFromDate = @"no date";
    }
    NSString *dueDate = [[NSString alloc] initWithFormat:@"%@%@", format, stringFromDate ];
    cell.detailTextLabel.text = dueDate;
    
    
    
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Tasks *task = [self.tasks.tasksArr objectAtIndex:indexPath.row];
    task.isDone = NO;
    if([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark)
    {
        task.isDone = YES;
    }
    
    if(task.isDone)
    {
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    }
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    if ([self.tableView isEditing]) {
        [self.tableView setEditing:NO animated:YES];
        self.navigationItem.rightBarButtonItem.title = @"Edit";
        [self.tableView reloadData];
    }
    else {
        self.navigationItem.rightBarButtonItem.title = @"Done";
        [self.tableView setEditing:YES animated:YES];
        [self.tableView reloadData];
    }
    
    
}

//editing tableRows
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//editing the table view
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.tasks.tasksArr removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

//rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

 //rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
     [self.tasks reorderTasksFromIndex:fromIndexPath.row toIndex:toIndexPath.row];
     
 }


 











@end
